﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using justice.Models;

namespace justice.Controllers
{
    [MyAuthorize(IsCheck = false)]
    [decide(IsCheck = false)]
    public class AdminController : Controller
    {
        justiceEntities dal = new justiceEntities();
        public ActionResult LoginIndex()
        {
            return View();
        }
        public ActionResult Login(string UserName,string UserPwd)
        {
            justice_user user = dal.justice_user.ToList().Find(l => l.userName == UserName&&l.password==UserPwd);
            if (user != null)
            {
                Role role = dal.Role.ToList().Find(l=>l.id == user.role_id);
                Session["uid"] = user.id;
                Session["username"] = user.userName;
                Session["rolename"] = role.RoleName;
                if (user.unit_id ==null && user.insurer_id== null)
                {
                    Session["unitname"] = "超级管理员";
                }
                else if(user.unit_id != null)
                {
                    judiciary jdc = dal.judiciary.ToList().Find(l => l.id == user.unit_id);
                    Session["unitname"] = jdc.judiciaryName;
                }
                else
                {
                    inserer jdc = dal.inserer.ToList().Find(l => l.insurer_id == user.insurer_id);
                    Session["unitname"] = jdc.insurerName;
                }
                System.Web.Security.FormsAuthentication.SetAuthCookie("hehe", false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("LoginError", "用户名或密码错误");
                return View("LoginIndex");
            }
        }
    }
}